package com.fastjars.business.captcha;

import org.patchca.background.BackgroundFactory;
import org.patchca.background.SingleColorBackgroundFactory;
import org.patchca.color.ColorFactory;
import org.patchca.color.GradientColorFactory;
import org.patchca.color.RandomColorFactory;
import org.patchca.color.SingleColorFactory;
import org.patchca.filter.FilterFactory;
import org.patchca.filter.predefined.*;

import java.awt.*;
import java.util.Properties;

/**
 * Config
 *
 * @author Brant Liu <br> 邮箱：<br>lbf1988@qq.com <br>日期：<br>2018/06/12
 * @version 1.0.0
 */
public class Config {
    private Properties properties;
    private ConfigHelper helper;

    public final static String CAPTCHA_SESSION_KEY = "CAPTCHA_SESSION_KEY";
    public final static String CAPTCHA_SESSION_DATE = "CAPTCHA_SESSION_DATE";
    public final static String CAPTCHA_SESSION_CONFIG_KEY = "captcha.session.key";
    public final static String CAPTCHA_SESSION_CONFIG_DATE = "captcha.session.date";

    public final static String CAPTCHA_FILTER_TYPE = "captcha.filter.type";

    public final static String CAPTCHA_FONT_COLOR_TYPE = "captcha.font.color.type";
    public final static String CAPTCHA_FONT_COLOR = "captcha.font.color";

    public final static String CAPTCHA_BACKGROUND_COLOR = "captcha.background.color";

    public static final String CAPTCHA_IMAGE_WIDTH = "captcha.image.width";
    public static final String CAPTCHA_IMAGE_HEIGHT = "captcha.image.height";

    public Config(Properties properties) {
        this.properties = properties;
        this.helper = new ConfigHelper();
    }

    public BackgroundFactory getBackgroundFactory() {
        String paramValue = this.properties.getProperty(CAPTCHA_BACKGROUND_COLOR);
        Color color = this.helper.getColor(CAPTCHA_BACKGROUND_COLOR, paramValue, Color.WHITE);
        return new SingleColorBackgroundFactory(color);
    }

    /**
     * 涟波滤
     *
     * @return
     */
    public FilterFactory getFontFilterFactory() {
        String paramValue = this.properties.getProperty(CAPTCHA_FILTER_TYPE);
        int filterType = this.helper.getPositiveInt(CAPTCHA_FILTER_TYPE, paramValue, 1);
        FilterFactory filterFactory;
        switch (filterType) {
            case 1:
            default:
                filterFactory = new CurvesRippleFilterFactory(getFontColorFactory());
                break;
            case 2:
                filterFactory = new MarbleRippleFilterFactory();
                break;
            case 3:
                filterFactory = new DoubleRippleFilterFactory();
                break;
            case 4:
                filterFactory = new WobbleRippleFilterFactory();
                break;
            case 5:
                filterFactory = new DiffuseRippleFilterFactory();
                break;
        }
        return filterFactory;
    }

    /**
     * 获取字体颜色
     *
     * @return
     */
    public ColorFactory getFontColorFactory() {
        String colorParamValue = this.properties.getProperty(CAPTCHA_FONT_COLOR);
        Color color = this.helper.getColor(CAPTCHA_FONT_COLOR, colorParamValue, new Color(25, 60, 170));

        String paramValue = this.properties.getProperty(CAPTCHA_FONT_COLOR_TYPE);
        int fontType = this.helper.getPositiveInt(CAPTCHA_FONT_COLOR_TYPE, paramValue, 1);
        ColorFactory colorFactory;
        switch (fontType) {
            case 1: {
                //单色黑色
                colorFactory = new SingleColorFactory(Color.BLACK);
            }
            break;
            case 2: {
                //多色
                colorFactory = new GradientColorFactory();
            }
            break;
            case 3: {
                //随机色
                colorFactory = new RandomColorFactory();
            }
            break;
            default: {
                colorFactory = new SingleColorFactory(color);
            }
            break;
        }
        return colorFactory;
    }

    public String getSessionKey() {
        return this.properties.getProperty(CAPTCHA_SESSION_CONFIG_KEY, CAPTCHA_SESSION_KEY);
    }

    public String getSessionDate() {
        return this.properties.getProperty(CAPTCHA_SESSION_CONFIG_DATE, CAPTCHA_SESSION_DATE);
    }

    public int getWidth() {
        String paramValue = this.properties.getProperty(CAPTCHA_IMAGE_WIDTH);
        return this.helper.getPositiveInt(CAPTCHA_IMAGE_WIDTH, paramValue, 150);
    }

    public int getHeight() {
        String paramValue = this.properties.getProperty(CAPTCHA_IMAGE_HEIGHT);
        return this.helper.getPositiveInt(CAPTCHA_IMAGE_WIDTH, paramValue, 50);
    }

    public Properties getProperties() {
        return properties;
    }
}
